package com.sky.wsp.mongodb;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Tag;

import javax.annotation.Resource;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author wangshaopeng@talkweb.com.cn
 * @desccription 在此输入描述
 * @date 2023/10/10
 */
@RestController
public class TestController {
    @Resource
    private InterfaceService interfaceService;

    @GetMapping("/get")
    public String get(Integer id) {
        Interface anInterface = interfaceService.get(id);
        System.out.println(anInterface);
        return "get";
    }

    @GetMapping("/list")
    public String list() {
        return "list";
    }

    public static void main(String[] args) {
        String yamlStr = getYaml();
        Yaml yaml = new Yaml();
        LinkedHashMap<String, Object> config = yaml.load(yamlStr);
//        LinkedHashMap<String, Object> spring = (LinkedHashMap<String, Object>)config.get("spring");
//        LinkedHashMap<String, Object> cloud = (LinkedHashMap<String, Object>)spring.get("cloud");
//        LinkedHashMap<String, Object> gateway = (LinkedHashMap<String, Object>)cloud.get("gateway");
        LinkedHashMap<String, Object> gateway = (LinkedHashMap<String, Object>)config.get("gateway");
        ArrayList<LinkedHashMap<String, Object>> routes = (ArrayList<LinkedHashMap<String, Object>>)gateway.get("routes");

        LinkedHashMap<String, Object> myRoute = new LinkedHashMap<>();
        myRoute.put("id", "abc");
        myRoute.put("uri", "https://abc.com");
        routes.add(myRoute);

//        String s = yaml.dumpAs(config, Tag.MAP, DumperOptions.FlowStyle.BLOCK);
        String s = yaml.dumpAsMap(config);
        System.out.println(s);
    }

    public static String getYaml() {
        return
                "gateway:\n" +
                "  routes:\n" +
                "    - id: wutong-ai-annotation\n" +
                "      uri: https://idaas.devcloud.talkweb.com.cn\n" +
                "      predicates:\n" +
                "        - Path=/wutong-ai-annotation/**\n" +
                "      filters:\n" +
                "        - StripPrefix=1\n";
    }
}
